package prototype;

public class StopBehaviour implements Runnable {

	private Agent agent;
	private static boolean released;

	public StopBehaviour(Agent a) {
		agent = a;
		released = false;
	}

	@Override
	public void run() {
		while (true) {
			synchronized (agent) {
				if (!agent.moving()) {
					agent.notifyAll();
				} else {
					while (!released)
						try {
							agent.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					if (agent.moving())
						agent.stop();
					agent.notifyAll();
				}
			}
		}
	}

	public static void release() {
		released = true;
	}

	public static void unrelease() {
		released = false;
	}
}
